let prices = [1, 2];
var maxProfit = function (prices) {
    let min = Infinity;
    let max = 0;

    for (let i = 0; i < prices.length; i++) {
        // 更新最小值
        if (prices[i] < min) {
            min = prices[i];
        }
        // 计算当前利润
        let num = prices[i] - min;
        // 更新最大利润
        if (max < num) {
            max = num;
        }
    }
    return max;
};

console.log(maxProfit(prices));